/**
 * @author lteng
 * @time 2022-06-18 14:28:45 
 */
 
package liteng.study.cn.stuscore.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.dao.ExamDao;
import liteng.study.cn.stuscore.entity.Exam;

public class ExamDaoImpl extends BaseDao implements ExamDao {

	@Override
	public void add(Exam exam) {
		String sql = "INSERT INTO exam(name,intro,epid,etid,gid)" + " VALUES (?,?,?,?,?)";
		add(sql, exam.getName(),exam.getIntro(),exam.getEpid(),exam.getEtid(),exam.getGid());

	}

	@Override
	public void update(Exam exam) {
		// TODO Auto-generated method stub
		String sql = "UPDATE exam SET name=?,intro=?,epid=?,etid=?,gid=? WHERE id=?";
		update(sql, exam.getName(), exam.getIntro(),exam.getEpid(),exam.getEtid(),exam.getGid(), exam.getId());

	}

	@Override
	public void delete(Exam exam) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM exam WHERE id=?";
		delete(sql, exam.getId());
	}

	@Override
	public Exam qryOne(int id) {
		// TODO Auto-generated method stub
		String sql = "SELECT id,name,intro,epid,etid,gid FROM exam WHERE id=?";

		ResultSet rs = getResl(sql, id);

		Exam exam = null;

		try {
			while (rs.next()) {
				exam = new Exam();
				exam.setId(rs.getInt("id"));
				exam.setName(rs.getString("name"));
				exam.setIntro(rs.getString("intro"));
				exam.setEpid(rs.getInt("epid"));
				exam.setEtid(rs.getInt("etid"));
				exam.setGid(rs.getInt("gid"));

			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return exam;
	}

//	@Override
//	public Exam qryExamByName(String name) {
//		// TODO Auto-generated method stub
//		String sql = "SELECT id,name,comm FROM exam WHERE name=?";
//
//		ResultSet rs = getResl(sql, name);
//
//		Exam exam = null;
//
//		try {
//			while (rs.next()) {
//				exam = new Exam();
//				exam.setId(rs.getInt("id"));
//				exam.setName(rs.getString("name"));
//				exam.setComm(rs.getString("comm"));
//
//			}
//		} catch (SQLException e) {
//			// TODO: handle exception
//			e.printStackTrace();
//		}
//		return exam;
//	}

	@Override
	public Page qryExams(int pageIndex, int pageSize, String name) {
		// TODO Auto-generated method stub
		Page page = new Page();

		String sql = "SELECT id,name,intro,epid,etid,gid"
				+ " FROM exam WHERE name LIKE CONCAT('%',?,'%') LIMIT ?,?";

		List<Map<String, Object>> resl = new ArrayList<Map<String, Object>>();

		ResultSet rs = getResl(sql, name, (pageIndex - 1) * pageSize, pageSize);

		try {
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();

				map.put("id", rs.getInt("id"));
				map.put("name", rs.getString("name"));
				map.put("intro", rs.getString("intro"));
				map.put("epid", rs.getInt("epid"));
				map.put("etid", rs.getInt("etid"));
				map.put("gid", rs.getInt("gid"));
	
				resl.add(map);
			}
			
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		String countSql = "SELECT count(*) FROM exam WHERE name LIKE CONCAT('%',?,'%')";

		int count = qryCount(countSql,name);

		int pageTotal = count % pageSize == 0 ? (count / pageSize) : (count / pageSize + 1);
		page.setCount(count);
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		page.setPageTotal(pageTotal);
		page.setData(resl);

		return page;
	}

	@Override
	public Page qryExams(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		Page page = new Page();

		String sql = "SELECT id,name,intro,epid,etid,gid FROM exam LIMIT ?,?";

		List<Map<String, Object>> resl = new ArrayList<Map<String, Object>>();

		ResultSet rs = getResl(sql, (pageIndex - 1) * pageSize, pageSize);

		try {
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
	
				map.put("id", rs.getInt("id"));
				map.put("name", rs.getString("name"));
				map.put("intro", rs.getString("intro"));
				map.put("epid", rs.getInt("epid"));
				map.put("etid", rs.getInt("etid"));
				map.put("gid", rs.getInt("gid"));
	
				resl.add(map);
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		String countSql = "SELECT count(*) FROM exam";

		int count = qryCount(countSql);

		int pageTotal = count % pageSize == 0 ? (count / pageSize) : (count / pageSize + 1);
		page.setCount(count);
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		page.setPageTotal(pageTotal);
		page.setData(resl);

		return page;
	}

//	@Override
//	public List<Exam> qryExams() {
//		// TODO Auto-generated method stub
//		String sql ="SELECT id,name,comm FROM exam";
//		ResultSet rs = getResl(sql);
//		List<Exam> exams = new ArrayList<Exam>();
//		try {
//			while (rs.next()) {
//				Exam exam = new Exam();
//				exam.setId(rs.getInt("id"));
//				exam.setName(rs.getString("name"));
//				exam.setComm(rs.getString("comm"));
//				
//				exams.add(exam);
//			}
//		} catch (SQLException e) {
//			// TODO: handle exception
//		}
//		return exams;
//	}

}
